package com.zzh.myapplication.tools;

import android.util.Log;

import net.icarplus.car.app.MyApplication;
import net.icarplus.car.constant.Content;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.util.Map;


public class HttpService {
    private static HttpClient httpClient;
    private static int TIME_OUT = 150 * 1000;// 设置请求超时15秒
    private static int SO_TIME = 300 * 1000;// 设置等待数据超时15秒

    /**
     * 设置请求超时以及等待数据超时时间
     *
     * @return
     */
    private static DefaultHttpClient getHttpClient() {
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIME_OUT);
        HttpConnectionParams.setSoTimeout(httpParams, SO_TIME);
        DefaultHttpClient client = new DefaultHttpClient(httpParams);
        ClientConnectionManager mgr = client.getConnectionManager();
        HttpParams params = client.getParams();
        client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,
                mgr.getSchemeRegistry()), params);
        return client;
    }

    public static String HttpsPostData(String url, Map<String, String> param,
                                       File file) throws Exception {
        String result = "";
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,
                Content.HTTPREQUESTTIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, Content.HTTPTIMEOUT);
        httpClient = HttpUtil.initClientHttps(httpParams);
        MultipartEntity entity = new MultipartEntity();
        // 添加文件参数
        if (file != null && file.exists()) {
            entity.addPart("filedata", new FileBody(file));
            // intpu={\"custId\":\"10001\",\"relaTable\":\"c_cust\",\"relaId\":\"10001\"}"
            entity.addPart("input",
                    new StringBody(MyApplication.gson.toJson(param)));
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(entity);
        HttpResponse httpResponse;
        httpResponse = httpClient.execute(httpPost);
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new RuntimeException("请求超时");
        } else {
            int code = httpResponse.getStatusLine().getStatusCode();
            if (code == 200) {
                String rev = EntityUtils.toString(httpResponse.getEntity());
                if (rev != null) {
                    result = rev;
                } else {
                    result = "返回结果为空";
                }
            }
        }
        Log.e("附件上传结果：", result);
        return result;
    }


    /**
     * 添加多附件
     *
     * @param url
     * @param param
     * @param fileParam
     * @return
     * @throws Exception
     */

    public static String HttpsPostData(String url, Map<String, Object> param,
                                       Map<String, String> fileParam) throws Exception {
        Log.e("url：", url);
        String result = "";
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,
                Content.HTTPREQUESTTIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, Content.HTTPTIMEOUT);
        httpClient = HttpUtil.initClientHttps(httpParams);
        MultipartEntity entity = new MultipartEntity();
        // 添加文件参数
        entity.addPart("input",
                new StringBody(MyApplication.gson.toJson(param)));
        if (fileParam != null && !fileParam.isEmpty()) {
            for (Map.Entry<String, String> entry : fileParam.entrySet()) {
                if (entry.getValue() != null) {
                    File file = new File(entry.getValue());
                    if (file.exists()) {
                        Log.e("entry：", entry.toString());
                        entity.addPart(entry.getKey(), new FileBody(file));
                    }

                }
            }
        }
        Log.e("Long", "entity:" + entity.toString());
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(entity);
        HttpResponse httpResponse;
        httpResponse = httpClient.execute(httpPost);

        if (httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
            String rev = EntityUtils.toString(httpResponse.getEntity());
            if (rev != null) {
                result = rev;
                System.out.println("result;;;;;;" + result);
            } else {
                result = "返回结果为空";
            }
        }
        Log.e("附件上传结果：", result);
        return result;
    }


}
